package edu.nyu.hps.assignment4;

import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileInputStream fin;
		FileOutputStream fout;
		BufferedReader in;
		// BufferedReader inFileName;
		String line;
		List<String> dataForPeople = new ArrayList<String>(); // Stores each
																// line
		List<String> dataForHospital = new ArrayList<String>();
		List<String> tmpArray = new ArrayList<String>();
		int bestSaved = 0;
		double startTime = System.currentTimeMillis()/1000;
		try {
			// System.out.println("Enter the filename: ");
			// inFileName = new BufferedReader(new
			// InputStreamReader(System.in));
			fin = new FileInputStream(args[0]);
//			fin = new FileInputStream(
//					"C:\\Users\\hiralrajani\\workspace\\Heuristics\\src\\edu\\nyu\\hps\\new\\input");
			in = new BufferedReader(new InputStreamReader(fin));

			// Reading Input for Person
			while (in.readLine().regionMatches(0, "p", 0, 1)) {
				break;
			}
			line = in.readLine();
			while (!line.equalsIgnoreCase("")) {
				dataForPeople.add(line);
				line = in.readLine();
			}
			// Readind Data for Hospital
			while (in.readLine().regionMatches(0, "h", 0, 1)) {
				break;
			}
			in.readLine();
			while (((line = in.readLine()) != null)) { 
				dataForHospital.add(line);
			}
			in.close();
			fin.close();						
			
			Computation obj = new Computation(dataForPeople, dataForHospital);
			for(int i=0;i<100;i++){
				for(int j=0;j<100;j++){
					Computation.grid[i][j] = 50;
				}
			}


			System.out.println("Saving Please wait...");
			while(System.currentTimeMillis()/1000 -  startTime <  20){
	
				obj.startSaving();
			}
			
//			System.out.println("The Routes are: ");
//			fout = new FileOutputStream("C:\\Users\\hiralrajani\\workspace\\Heuristics\\src\\edu\\nyu\\hps\\assignment4\\output");
			fout = new FileOutputStream(args[1]);
			for (int i = 0; i < obj.routes.size(); i++) {
				new PrintStream(fout).println (obj.routes.get(i));
//				System.out.println(obj.routes.get(i));
			}
//			System.out.println("Total Saved : " + obj.totalSaved);
			fout.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
